<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?
    include_once "src/facebook.php";
    $fbconfig['appid' ] = "211898488879874";
    $fbconfig['secret'] = "dd78ca2d0974f92f53e125c831cb36fb";
    $fbconfig['baseurl'] = "http://sentyfb.altervista.org";
   
?>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Happy Birthday!!!</title>
</head>
<body>
<br /><br />
<?

class Message {

  public $message;
  public $nbLikes, $nbCom;
  public $comments;
  public $IDLikes;
 
  public function __construct() { }


}
        //Facebook Authentication
        //LOGIN
        $code = $_GET['code'];
       
        if(empty($code))
        {
            $dialog_url = "http://www.facebook.com/dialog/oauth?client_id="
           . $fbconfig['appid'] . "&redirect_uri=http://qualsiasicosa.altervista.org/adrien.php&scope=user_birthday,friends_birthday,publish_stream,read_stream";
       
           echo("<a href=" . $dialog_url . ">LOGIN</a>");
        }
        
        //TOKEN
        else
        {
            $token_url = "https://graph.facebook.com/oauth/access_token?"."client_id=" . $fbconfig['appid'] . "&redirect_uri=http://qualsiasicosa.altervista.org/adrien.php&client_secret=" . $fbconfig['secret'] . "&code=" . $code;
   
            $response = file_get_contents($token_url);
            $params = null;
            parse_str($response, $params);

            $graph_url = "https://graph.facebook.com/me?access_token=".$params['access_token'];

            $user = json_decode(file_get_contents($graph_url));
           
           
           
            ?>
            <form method="POST" action="./prova.php">
                <input type="hidden" name="token" value=<? echo($params['access_token']); ?> />
                <input type="hidden" name="id" value=<? echo($user->id); ?> />
                <input type="text" name="message" value="" />
                <input type="submit" name="sub" value="SUBMIT" />
                  <br />
            </form>
            <?
                       
            echo("HELLO  ". $user->name);?><br/><br/><?
           
           
            echo("POSTS: ");?><br/><br/><?
           
            $graph_url = "https://graph.facebook.com/me/statuses?access_token=".$params['access_token'];

            $post = json_decode(file_get_contents($graph_url));


            $userMessage = array_fill(0, count($post->data), new Message());
           
            for($i = 0 ; $i < count($post->data)  ; $i++)
            {
                $userMessage[$i]->message = $post->data[$i]->message;
                ?><h3><?
                echo $userMessage[$i]->message;
                ?></h3><br><?
               
                for($j = 0 ; $j < count($post->data[$i]->likes->data); $j++)
                {
                     $userMessage[$i]->nbLikes = $j;
                }
                if (!$userMessage[$i]->nbLikes)
                    $userMessage[$i]->nbLikes = 0;
                ?><br><?
                //getting number of comments
                $userMessage[$i]->nbCom = count($post->data[$i]-> comments->data);
                if(!$userMessage[$i]->nbCom)
                    $userMessage[$i]->nbCom = 0;
                   
                echo "Number of likes : ".$userMessage[$i]->nbLikes;
                ?><br><?
                echo "Number of comments: ".$userMessage[$i]->nbCom;
                ?><br><?
               
               
                for($j = 1 ; $j <= count($post->data[$i]-> comments->data); $j++)
                {
                $userMessage[$i]->comments[$j]=$post->data[$i]->comments->data[$j-1]->message;
                echo "Comment ".$j;
                echo " : ".$userMessage[$i]->comments[$j];
               
                ?><br><?
                }
                ?><br><br><br><?
       
            }
            }
   
?>
</body>
</html>